package com.fzu.normalservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.fzu.commonutil.entity.ServiceType;
import com.fzu.commonutil.params.ListParams;
import com.fzu.commonutil.util.ApiException;
import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.util.R;
import com.fzu.commonutil.entity.Company;
import com.fzu.normalservice.service.CompanyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/company")
@RequiredArgsConstructor
public class CompanyController {
    private final CompanyService companyService;

    /**
     * 公司选项
     * @return
     */
    @GetMapping("/options")
    public R options(Company company) {
        return R.ok().data(companyService.options(company));
    }

    /**
     * 获取id-实体类map
     * @param params
     * @return
     */
    @PostMapping("/map")
    public Map<Integer, String> map(@RequestBody ListParams params) {
        return companyService.map(params.getIdList());
    }

    /**
     * 获取公司详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        return R.ok().data(companyService.getById(id));
    }
    /**
     * 获取公司
     * @return
     */
    @GetMapping
    public R list(Company company,
                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return R.ok().data(companyService.page(company, pageNum,pageSize));
    }

    /**
     * 增加公司
     * @param company
     * @return
     */
    @PostMapping
    public R add(@RequestBody Company company){
        companyService.add(company);
        return R.ok();
    }
    /**
     * 更新公司信息
     * @param company
     * @return
     */
    @PutMapping
    public R update(@RequestBody Company company) {
        Integer id = company.getId();
        if(id != null){
            companyService.updateById(company);
        }else {
            throw new ApiException("ID值不能为空");
        }

        return R.ok();
    }

    @GetMapping("/getByCompanyName")
    public R getAdminByCompanyName(@RequestParam String companyName) {
        Integer userId = companyService.getAdminByCompanyName(companyName);
        return R.ok().data(userId);
    }

    /**
     * 获取所有公司
     * @return
     */
    @GetMapping("/all")
    public List<Company> all() {
        return companyService.list(new LambdaQueryWrapper<>());
    }
}

